package vn.conghuy.talkwithme.responsor.responsor;

import java.util.ArrayList;

import android.content.Context;

import vn.conghuy.talkwithme.R;
import vn.conghuy.talkwithme.data.sqlmodel.SqlData;
import vn.conghuy.talkwithme.data.wolfram.WolframAssumtion;
import vn.conghuy.talkwithme.data.wolfram.WolframPod;
import vn.conghuy.talkwithme.data.wolfram.WolframService;
import vn.conghuy.talkwithme.utils.Utils;

public class WolframResponsor extends Responsor {

	public static final int RELATE_NONE = 0;
	public static final int RELATE_ASK_MORE_INFO = 1;
	public static final int RELATE_OTHER_INFO = 2;
	public static final int RELATE_DETAIL_INFO = 3;
	
	
	WolframService wolService;
	WolframAssumtion assumption;
	String originRequest;
	WolframPod requestDetailPod;
	
	public WolframResponsor(String apiKey, Context context) {
		super(context);
		wolService = new WolframService(apiKey);
	}
	@Override
	public String response(String request) {
		
		if(Utils.isExistStringInArray(SqlData.stopWords, request) == true){
			relativeLastResponse = RELATIVE_NONE;
			return Utils.getRandomString(SqlData.callCancelPhrases);
		}
		
		String sResponse = "";
		WolframPod pod = null;
		
		//if this reques don't relate with last request
		switch(relativeLastResponse){
		case RELATE_NONE:
			originRequest = request;
			boolean result = wolService.request(request);
			if(result == true)
				pod = wolService.getFirstResult();
			else return null;
			break;
		case RELATE_DETAIL_INFO:
			pod = requestDetailPod;
			break;
		case RELATE_ASK_MORE_INFO:
			// try get pod contains info in request. If not, get next pod.
			pod = wolService.getPod(request);
			if(pod == null){
				pod = wolService.nextPod();
			}
			break;
		case RELATE_OTHER_INFO:
			WolframAssumtion assum = wolService.getAssumption(request);
			if(assum != null){
				String newRequest = originRequest+"&assumption="+assum.getInput();
				result = wolService.request(newRequest);
				if(result == true)
					pod = wolService.getFirstResult();
			}
			break;
		}
		
		if(pod != null){
			sResponse = "<b>" + pod.getTitle() + " : </b><br>";
			String plainText = pod.getPlainText();
			if(plainText.equals("")){
				sResponse += "<center>" + pod.getImage() + "</center>";
			}
			else{
				sResponse += plainText;
			}
		}
		else{
			sResponse = null;
		}
		
		
		if(sResponse != null){
			sResponse = sResponse.replace("\n", "<br>");
			sResponse = sResponse.replace("|", ":");
		}
		/*else{
			// if user ask more detail (in RELATE_ASK_MORE_INFO case), but has no more information
			if(relativeLastResponse == RELATE_ASK_MORE_INFO){
				relativeLastResponse = RELATIVE_NONE;
				return context.getString(R.string.wolfram_has_no_more_information);
			}
		}*/
		
		lastRequest = request;
		return sResponse;
	}

	@Override
	public int relativeWithLastRequest(String request) {
		
		// relative more information when request contain podtitle
		// when request contain pod title or opposite direction
		
		ArrayList<WolframPod> pods = wolService.getPods();
		for(WolframPod pod: pods){
			if(pod.getTitle().toLowerCase().contains(request.toLowerCase()) 
					|| request.toLowerCase().contains(pod.getTitle().toLowerCase())){
				requestDetailPod = pod;
				relativeLastResponse = RELATE_DETAIL_INFO;
				return relativeLastResponse;
			}
		}
		
		
		// relative when user ask more infomation or ask other info.
		// when request has a "wolfram more info" keyword, it will occur
		
		boolean isAskMoreAction = Utils.isExistElemInArrayIsWordOfStr(SqlData.wolframMoreInfoAction, request, null);
		if(isAskMoreAction == true)
		{
			relativeLastResponse = RELATE_ASK_MORE_INFO;
			return relativeLastResponse;
		}
		
		// request has a string of assumption.
		
		ArrayList<WolframAssumtion> assumptions = wolService.getassumptions();
		for(WolframAssumtion ass :assumptions){
			String name = ass.getName();
			String desc = ass.getDesc();
			
			if(request.toLowerCase().contains(name.toLowerCase())
					|| name.toLowerCase().contains(request.toLowerCase()) 
					|| request.toLowerCase().contains(desc.toLowerCase())
					|| desc.toLowerCase().contains(request.toLowerCase())){
				assumption = ass;
				relativeLastResponse = RELATE_OTHER_INFO;
				return relativeLastResponse;
			}
		}
		
		relativeLastResponse = RELATE_NONE;
		return relativeLastResponse;
	}

}
